var recordAlbumDetails:Object;
var lock:Boolean;
var chapterListNames:Object;
var recordTaskList:Object;

function init(param, data, slot)
{
	Packages.model.Context.activeContent=this;

	// UI.init
	var basic=Packages.system.mlf.GateKeeper.scanElement(this._name, "basic");
	Packages.system.display.JAccordion.initialize(basic);
	
	// Render.init
	// Packages.system.render.Filter.titleFilter(Packages.system.mlf.GateKeeper.scanElement(this._name, "bookSpecialTitle"));
	// Packages.system.render.Filter.titleFilter(Packages.system.mlf.GateKeeper.scanElement(this._name, "bookPageTitle"));
	Packages.system.display.SceneTransition.rollOut();

	// Event.init
	var evtTargetArr:Array=new Array();
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "updateAlbumSpot"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "enterRecordingSpot"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "enterPostProSpot"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "enterPubSpot"));
	Packages.system.events.StateFulButtonEvent.initialize(evtTargetArr);

	invokeInitComplete();
}

function dispose()
{
	Packages.system.mlf.GateKeeper.removeListener("onReleaseButtonEvent", this);
	
	delete recordAlbumDetails;
	delete chapterListNames;
	delete recordTaskList;
}

function invokeInitComplete()
{
	Packages.system.mlf.GateKeeper.addListener("onReleaseButtonEvent", this);
	
	loadAlbumList();
}

function loadAlbumList()
{
	Packages.system.display.Progress.createInstance(Packages.system.mlf.GateKeeper.scanElement(this._name, "albumList"));

	// method
	Packages.modula.RequestCommand.request("HTTP", "restlet/members/me/recordAlbums?startIndex=-1&count=-1", null, Packages.model.vo.AlbumListView, "normal", this, {container:"albumList", saved:"recordAlbumDetails", slot:this._name, uri:"app/content/myReadjoy/RecordingSpace.xml"});
}

function loadTaskList(albumId)
{
	Packages.log("loadTaskList ", "debug");
	Packages.system.display.Progress.createInstance(Packages.system.mlf.GateKeeper.scanElement(this._name, "albumList"));

	Packages.modula.RequestCommand.request("HTTP", "restlet/recordalbums/"+albumId+"/recordtasks?startIndex=-1&count=-1", null, Packages.model.vo.AlbumView, "normal", this, {container:"albumList", chapterListNames:chapterListNames, albumDetails:recordAlbumDetails, selectedAlbumId:albumId, slot:this._name, invokeCall:"loadTaskListEnd", uri:"app/content/myReadjoy/RecordingSpace.xml", saved:"recordTaskList"});
}

function loadTaskListEnd()
{
	Packages.log("loadTaskListEnd ", "debug");
	lock=false;

	var relatedAlbumInfo=Packages.system.mlf.GateKeeper.scanElement(this._name, "relatedAlbumInfo");
	if (relatedAlbumInfo._visible) return;
	Packages.system.mlf.GateKeeper.updatePortletView(this._name, "relatedChapterInfo", "relatedAlbumInfo");
}

function loadChapterInfo(chapterId:String)
{
	Packages.log("loadChapterInfo: "+chapterId, "debug");
	
	var roomId:String;
	var content:Object=new Object();

	var chapterName_tf=Packages.system.mlf.GateKeeper.scanElement(this._name, "chapterName_"+chapterId);

	content.chapterName=chapterName_tf.text;
	content.ccreateTime="";
	content.updateTime="";
	content.member="";
	content.finishProg="0";
	content.taskRate="";

	for (var i=0; i<recordTaskList.count; i++)
	{
		var taskItem=recordTaskList.objects[i];
		Packages.log("chapter.chapterId: "+taskItem["chapter.chapterId"], "debug");
		if (taskItem["chapter.chapterId"]==chapterId)
		{
			content.ccreateTime=taskItem.createTime;
			content.updateTime=taskItem.updateTime;
			// taskRate
			// member
			content.finishProg=taskItem.status;
			Packages.log("taskItem.status: "+taskItem.status, "debug");

			roomId=taskItem["recordAlbum.recordAlbumId"]+"_"+chapterId;

			break;
		}
	}

	for (var i=0; i<chapterListNames.count; i++)
	{
		if (chapterListNames.objects[i].chapterId==chapterId)
		{
			Packages.modula.task.Composition.contentId=chapterListNames.objects[i].uuid;
			break;
		}
	}

	// content.finishProg as condition for the enterSpot
	changeEnterSpotView(content.finishProg, roomId);	

	content.finishProg=Packages.system.mlf.GateKeeper.scanDictionary(chapterName_tf, "TASK_PROG_"+content.finishProg);

	Packages.system.mlf.GateKeeper.setValue(content, this._name);

	var relatedChapterInfo=Packages.system.mlf.GateKeeper.scanElement(this._name, "relatedChapterInfo");
	if (relatedChapterInfo._visible) return;
	Packages.system.mlf.GateKeeper.updatePortletView(this._name, "relatedAlbumInfo", "relatedChapterInfo");
}

function changeEnterSpotView(status:String, roomId:String)
{
	var enterRecordingSpotView:Boolean=true;
	var enterPostProSpotView:Boolean=true;
	var enterPubSpotView:Boolean=true;
	if (status=="0")
	{
		enterPostProSpotView=false;
		enterPubSpotView=false;
	}
	else if (status=="1")
	{
		enterPubSpotView=false;
	}

	var enterRecordingSpot=Packages.system.mlf.GateKeeper.scanElement(this._name, "enterRecordingSpot");
	enterRecordingSpot._visible=enterRecordingSpotView;

	var enterPostProSpot=Packages.system.mlf.GateKeeper.scanElement(this._name, "enterPostProSpot");
	enterPostProSpot._visible=enterPostProSpotView;

	var enterPubSpot=Packages.system.mlf.GateKeeper.scanElement(this._name, "enterPubSpot");
	enterPubSpot._visible=enterPubSpotView;

	// Permission on the scope and collab
	// condition
	Packages.model.UserProfile.RecordingCollabId=roomId;
}

function loadAlbumInfo(albumId:String)
{
	Packages.log("loadAlbumInfo: "+albumId, "debug");

	if (lock) return;
	lock=true;
	Packages.system.display.Progress.createInstance(Packages.system.mlf.GateKeeper.scanElement(this._name, "albumList"));
	
	var bookId:String;
	for (var i=0; i<recordAlbumDetails.count; i++)
	{
		if (recordAlbumDetails.objects[i].recordAlbumId==albumId)
		{
			bookId=recordAlbumDetails.objects[i]["book.bookId"];
			break;
		}
	}

	var args:Array=new Array();
	args.push(albumId);

	Packages.log("chapters: "+bookId, "debug");

	Packages.modula.RequestCommand.request("HTTP", "restlet/books/"+bookId+"/chapters?startIndex=-1&count=-1", null, null, "normal", this, {container:"albumList", saved:"chapterListNames", invokeCall:"loadTaskList", invokeArgs:args});
}

function onReleaseButtonEvent(evt:Object)
{
	if (evt.param=="updateAlbumSpot")
	{
	}
	else if (evt.param=="enterRecordingSpot")
	{
		Packages.components.SceneAgent.loadScene("content/myReadjoy/recording/Prepairation");
	}
	else if (evt.param.indexOf("aItem")>-1)
	{
		// selected one Album Item
		var selectedAlbumId:String=evt.param.substr(5);
		loadAlbumInfo(selectedAlbumId);
	}
	else if (evt.param.indexOf("cItem")>-1)
	{
		var selectedChapterId:String=evt.param.substr(5);
		loadChapterInfo(selectedChapterId);
	}
}

function handleResponseResult(rsp:Object)
{
	if (rsp.vo.instance.type=="Template")
	{
		// Packages.system.mlf.GateKeeper.updateDynamicPortletView(this._name, dynamicPortet, "commonRemotingResult", rsp.vo.instance.content);
		Packages.system.mlf.GateKeeper.loadML2byContent(rsp.vo.instance.content);
	}
	else if (rsp.vo.instance.type=="doWrapper")
	{	
		// Packages.log("rsp.vo.instance.content: "+rsp.vo.instance.content.tasks.data, "debug");
		
		Packages.system.mlf.GateKeeper.setValue(rsp.vo.instance.content, this._name);
	}

	if (rsp.component.data.saved!=null)
	{
		this[rsp.component.data.saved]=rsp.service.result;
	}

	if (rsp.component.data.invokeCall!=null)
	{
		this[rsp.component.data.invokeCall].apply(this, rsp.component.data.invokeArgs);
	}
	Packages.system.display.Progress.removeInstance(Packages.system.mlf.GateKeeper.scanElement(this._name, rsp.component.data.container));
}